package project.controllers.workers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;

import project.controllers.sort.SortOrderByDeadlineDate;
import project.model.IGarage;
import project.model.IOrder;
import project.model.OrderStatus;

import org.apache.log4j.Logger;

public class WorkerWithOrder {

	private static final Logger log = Logger.getLogger(WorkerWithOrder.class);
	private static final String ORDER_NUMBER = "Order #";
	private static final String PERFORMED_BY_MASTER = " performed by master ";
	private static final String NEXT_FREE_DATE = "Next free date: ";
	private static final String ORD_DATE_CHANGE = "Order's deadline change on ";

	static void getMastersByOrder(IOrder order) {
		System.out.println(ORDER_NUMBER + order.getNumberOrder()
				+ PERFORMED_BY_MASTER + order.getMaster().getNameMaster());
	}

	static void getListMadingOrder(ArrayList<IOrder> orderList,
			Comparator sortby) {
		try {
			ArrayList<IOrder> ordList = new ArrayList<IOrder>();
			Date date = new Date();
			for (IOrder ord : orderList) {
				if (ord.getBeginingDate().before(date)
						&& ord.getDeadlineDate().after(date)
						&& ord.getOrderStatus() == null) {
					ordList.add(ord);
				}
			}
			Collections.sort(ordList, sortby);
			for (IOrder order : ordList) {
				System.out.println(ORDER_NUMBER + order.getNumberOrder());
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}

	}

	static void SortOrder(ArrayList<IOrder> orderList, Comparator sortBy) {
		try {
			Collections.sort(orderList, sortBy);
			for (IOrder order : orderList) {
				System.out.println(ORDER_NUMBER + order.getNumberOrder());
			}

		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	static void getNextFreeDate(ArrayList<IOrder> orderList) {
		try {
			Collections.sort(orderList, new SortOrderByDeadlineDate());
			GregorianCalendar temp = orderList.get(0).getDeadlineDate();
			temp.add(GregorianCalendar.DAY_OF_YEAR, 1);
			System.out.println(NEXT_FREE_DATE
					+ temp.getTime().toLocaleString().substring(0, 10));
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	static void getOrderByPeriod(ArrayList<IOrder> orderList,
			Comparator sortby, OrderStatus ordSt, GregorianCalendar begin,
			GregorianCalendar end) {
		try {
			ArrayList<IOrder> ordList = new ArrayList<IOrder>();

			for (IOrder ord : orderList) {
				if ((ord.getBeginingDate().after(begin)
						&& ord.getBeginingDate().before(end) || ord
						.getDateStat().after(begin)
						&& ord.getDateStat().before(end))
						&& ord.getOrderStatus() == ordSt) {
					ordList.add(ord);
				}
			}
			Collections.sort(ordList, sortby);
			for (IOrder order : ordList) {
				System.out.println(ORDER_NUMBER + order.getNumberOrder());
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	static void addOrder(ArrayList<IOrder> orderList, IOrder order) {
		orderList.add(order);
	}

	static void markOrder(ArrayList<IOrder> orderList,
			ArrayList<IGarage> garageList, OrderStatus ordSt, IOrder order,
			GregorianCalendar nowDate) {
		try {
			for (IOrder o : orderList) {
				if (o.equals(order)) {
					o.setOrderStatus(ordSt);
					o.setDateStat(nowDate);
					o.getMaster().markMasterFree();
					for (IGarage garage : garageList) {
						if (garage.getOrder().equals(order)) {
							garage.markGarageEmpty();
						}
					}
				}
			}
		} catch (NullPointerException n) {
			log.warn("Collection(s) is(are) empty");

		}
	}

	static void ShirtTimeOrder(ArrayList<IOrder> orderList,
			GregorianCalendar newDate, IOrder order) {
		try {
			for (IOrder ord : orderList) {
				if (ord.equals(order)) {
					ord.shiftTime(newDate);
					System.out.println(ORD_DATE_CHANGE
							+ ord.getDeadlineDate().getTime().toLocaleString()
									.substring(0, 10));
				}
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

}
